import axios from "axios";
import { getToken } from "./setToken";
import { MessagePlugin } from "tdesign-vue-next";
import router from '../router'
const service = axios.create({
  baseURL: "/dc", 
  // baseURL:"http://192.168.1.217:8080/dc",
  timeout: 20000,
});
// 请求拦截
service.interceptors.request.use(
  (config) => {
    // 从缓存获取token放在请求头
    config.headers["token"] = getToken("token");
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);
// 响应拦截器
service.interceptors.response.use(
  (response) => {
    let { status, message } = response.data;
    console.log(response);
    if(response.data===''){
      setTimeout(()=>{
      router.push('/login')
      MessagePlugin.error("登录状态已过期，请重新登录！");
      return
      })
    }
    if (status !== 200) {
      // MessagePlugin.success("error");
    }
    return response.data;
  },
  (error) => {
    return Promise.reject(error);
  }
);

export default service;
